package tree;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class isBalanced {
    public boolean isBalanced(TreeNode root) {
        return getHeight(root) != -1;
    }
    public int getHeight(TreeNode root) {
        if (root == null){
            return 0;
        }
        int leftHeight = getHeight(root.left);
        if (leftHeight == -1){
            return -1;
        }
        int rightHeight = getHeight(root.right);
        if (rightHeight == -1){
            return -1;
        }
        int result = Math.abs(leftHeight - rightHeight);
        if (result > 1){
            return -1;
        }
        return Math.max(leftHeight, rightHeight) + 1;
    }
}
